unit SceneGame;

interface

uses DGLE, DGLE_Types, Engine, Scene, SceneManager, Button, Tile, Creature, Item,
  Map, IntBar, IntUseBar, Types;

type
  TSceneGame = class(TScene)
  private
    Map: TMap;
    Items: TItems;
    Creatures: TCreatures;
    Lifebar: TIntBar;
    Manabar: TIntBar;
    Invbar: TIntUseBar;
  public
    constructor Create;
    destructor Destroy; override;
    procedure Render(); override;
    procedure Update(); override;
  end;

implementation

{ TSceneGame }

uses SysUtils, uBox;

constructor TSceneGame.Create;
var
  I: Byte;
begin
  Map := TMap.Create;

  Items := TItems.Create;

  Items.Add(0, 0, 2);
  Items.Add(9, 7, 4);
  for I := 0 to 255 do
  begin
    Items.Add(2, 2, 0);
    Items.Add(4, 0, 1);
  end;

  Creatures := TCreatures.Create;

  Creatures.Add(5, 5, 0, fcAlly);
//  Creatures.Add(8, 5, 0, fcAlly);
//  Creatures.Add(6, 7, 0, fcAlly);
//  Creatures.Add(7, 8, 0, fcAlly);

  Creatures.Add(9, 6, 1, fcEnemy);
  Creatures.Add(11, 10, 1, fcEnemy);
  Creatures.Add(1, 1, 1, fcEnemy);
  Creatures.Add(2, 5, 1, fcEnemy);


  Lifebar := TIntBar.Create(10, SCREEN_HEIGHT - 28, 'Lifebar.png');
  Manabar := TIntBar.Create(SCREEN_WIDTH - 213, SCREEN_HEIGHT - 28, 'Manabar.png');
  Invbar := TIntUseBar.Create;
end;

destructor TSceneGame.Destroy;
begin
  Invbar.Free;
  Lifebar.Free;
  Manabar.Free;
  Creatures.Free;
  Items.Free;
  Map.Free;
  inherited;
end;

var
  A, B: Integer;

procedure TSceneGame.Render;
var
  W, H: Cardinal;
  I: Integer;
begin
  Map.Render(Creatures.Creature[CURRENT_ALLY].Pos.X, Creatures.Creature[CURRENT_ALLY].Pos.Y);

  TextOut(0, 0,  Round(MousePos.X), Color4(255, 0, 0, 255));
  TextOut(0, 20, Round(MousePos.Y), Color4(255, 0, 0, 255));
  TextOut(0, 40, Round(ScreenPos.X), Color4(255, 0, 0, 255));
  TextOut(0, 60, Round(ScreenPos.Y), Color4(255, 0, 0, 255));
  pEngineCore.GetDesktopResolution(W, H);
  TextOut(0, 80, Round(W), Color4(255, 0, 0, 255));
  TextOut(0, 100, Round(H), Color4(255, 0, 0, 255));
  TextOut(0, 120, 'Creatures: ' + IntToStr(Creatures.Count), Color4(255, 0, 0, 255));
  TextOut(0, 140, 'Items: ' + IntToStr(Items.Count), Color4(255, 0, 0, 255));
  TextOut(0, 160, 'Top: ' + IntToStr(MAP_TOP) +', Left: ' + IntToStr(MAP_LEFT), Color4(255, 0, 0, 255));
  TextOut(0, 180, 'Move: ' + IntToStr(A) + ':' + IntToStr(B), Color4(255, 0, 0, 255));
  TextOut(0, 200, 'Current: ' + IntToStr(CURRENT_ALLY), Color4(255, 0, 0, 255));

  Creatures.Render;
  Items.Render;

  Lifebar.Render(Creatures.Creature[CURRENT_ALLY].Life.Cur, Creatures.Creature[CURRENT_ALLY].Life.Max);
  Manabar.Render(Creatures.Creature[CURRENT_ALLY].Life.Cur, Creatures.Creature[CURRENT_ALLY].Life.Max);

  Invbar.Render;

  if not Creatures.Creature[CURRENT_ALLY].Life.IsMin then
    for I := 0 to 9 do
    begin
      Items.RenderItem(Invbar.Left + (I * 33) + 1, Invbar.Top + 1, I);
      pFont11.Draw2DSimple(Invbar.Left + (I * 33) + 24, Invbar.Top + 17, StrToPAChar(IntToStr(I)), Color4);
    end;
end;

var
  CR: Integer = 0;

procedure TSceneGame.Update;
var
  I, X, Y: Integer;
begin
  Creatures.Update;
  Items.Update;

  if RightButton then
  begin
    A := Round(ScreenPos.X) + MAP_LEFT;
    B := Round(ScreenPos.Y) + MAP_TOP;
    Creatures.Creature[CURRENT_ALLY].Target := Point(A, B);
  end;

  if LeftButton then
  begin
    A := Round(ScreenPos.X) + MAP_LEFT;
    B := Round(ScreenPos.Y) + MAP_TOP;
    for I := 0 to Creatures.Count - 1 do
    if Creatures.Creature[I].Active
    and (Creatures.Creature[I].Pos.X = A)
    and (Creatures.Creature[I].Pos.Y = B) then
    begin
      if (Creatures.Creature[I].Force = fcAlly) then
      begin
        CURRENT_ALLY := I;
      end;
    end;
  end;

  GameTick := GameTick + 0.9;
  if (GameTick > 1) then
  begin
    GameTick := 0.0;
    Inc(CR);
    if (CR > Creatures.Count - 1) then CR := 0;
    Creatures.Process(CR);
  end;

end;

end.
